package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.modules.shop.entity.GoodsMsg;
import io.renren.modules.shop.entity.GoodsPic;
import io.renren.modules.shop.service.GoodsMsgService;
import io.renren.modules.shop.service.GoodsPicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("goods_pic")
@Api(value = "图片地址controller", tags = {("图片地址接口")})
public class GoodsPicController {
    @Autowired
    private GoodsPicService goodsPicService;

    @Autowired
    private GoodsMsgService goodsMsgService;

    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@RequestBody GoodsPic goodsPic, @RequestParam Integer size, @RequestParam Integer page) {
        IPage<GoodsPic> iPage = new Page<>(page, size);
        IPage<GoodsPic> goodsCouponIPage = goodsPicService.page(iPage, new QueryWrapper<>(goodsPic));
        List<GoodsPic> list = goodsCouponIPage.getRecords();
        return R.ok().put("page", list);
    }


    @ApiOperation(value = "新增详情订单")
    @PostMapping("add")
    public R addNew(@RequestBody GoodsPic goodsPic) {
        boolean flag = goodsPicService.save(goodsPic);
        return R.ok().put("flag", flag);
    }

    @ApiOperation(value = "查询所有数据")
    @PostMapping("list")
    public R getList(@RequestBody GoodsPic goodsPic) {
        List<GoodsPic> list = goodsPicService.list(new QueryWrapper<>(goodsPic));
        return R.ok().put("list", list);
    }

    @ApiOperation(value = "根据ids删除记录")
    @PostMapping("delete")
    public R del(@RequestBody Long[] ids) {
        boolean flag = goodsPicService.removeByIds(Arrays.asList(ids));
        return R.ok().put("flag", flag);
    }

    /**
     *  上传图片
     * @param files
     * @param request
     * @return
     */
    @PostMapping("/uploadPic")
    public R uploadPic(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) {
        R r = goodsPicService.uploadImg(files, request);
        return r;
    }

    /**
     * 保存商品和对应图片地址
     * @param files    上传的文件字符串
     * @param request  HttpServletRequest
     * @param goodsMsg 上传的物品信息
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadMultipleFile")
    @ApiOperation(value = "上传图片和商品信息到图片服务器")
    @Transactional(rollbackFor = Exception.class)
    public R uploadImg(@ApiParam("图片文件地址") @RequestParam String files, HttpServletRequest request, @ApiParam("商品其他信息") @RequestBody GoodsMsg goodsMsg) throws IOException {
        if (goodsMsg == null) {
            return R.error("商品信息不能为空");
        }
        try {
            GoodsPic goodsPic = new GoodsPic();
            goodsMsgService.save(goodsMsg);
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            goodsPic.setPicPath(files);
            goodsPicService.save(goodsPic);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("/updateMsg")
    @ApiOperation(value = "修改更新商品信息")
    public R updatePic(@ApiParam("图片文件地址") @RequestParam String files, HttpServletRequest request, @ApiParam("商品其他信息") @RequestBody GoodsMsg goodsMsg) {
        if (goodsMsg == null) {
            return R.error("商品信息不能为空");
        }
        GoodsPic goodsPic = new GoodsPic();
        goodsMsgService.updateById(goodsMsg);
        goodsPic.setGoodsId(goodsMsg.getGoodsId());
        if (!"".equals(files)) {
            goodsPicService.remove(new QueryWrapper<>(goodsPic));
            goodsPic.setPicPath(files);
            goodsPicService.save(goodsPic);
        }
        return R.ok();
    }
}
